package commons.utils.primitive_geom;

import commons.spatialindex.I_SpatialIndexable;
import commons.utils.UtilsCalcul;
import commons.utils.UtilsGeom;

public class Point_d implements I_SpatialIndexable {

	public double x;
	public double y;

	public Point_d (double x, double y) {
		this.x = x;
		this.y = y;
	}
	public Point_d (Point_d pt) {
		this (pt.x, pt.y);
	}
	public Point_d (double x, double y, int arrondi) {
		this (UtilsCalcul.round(x, arrondi), UtilsCalcul.round(y, arrondi));
	}

	@Override
	public String toString() {
		return "( " + UtilsCalcul.round3(x) + " , " + UtilsCalcul.round3(y) + " )";
	}

	public boolean equals (Point_d pt) {
		return this==pt;
	}
	public boolean equalsXY (Point_d pt) {
		return this.x==pt.x && this.y==pt.y;
	}

	
	public Point_d getCopy() {
		return new Point_d(this.x, this.y);
	}

	public static double distPtPt(Point_d pt1, Point_d pt2) {
		return Math.sqrt ( (pt1.x-pt2.x)*(pt1.x-pt2.x) + (pt1.y-pt2.y)*(pt1.y-pt2.y) );
	}
	public static double distPtPt(double x1, double y1, double x2, double y2) {
		return Math.sqrt ( (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) );
	}
	
	public double distTo (Point_d pt) {
		return Math.sqrt ( (x-pt.x)*(x-pt.x) + (y-pt.y)*(y-pt.y) );
	}
	public double distTo_sq (Point_d pt) {
		return (x-pt.x)*(x-pt.x) + (y-pt.y)*(y-pt.y);
	}

	/**
	 * Renvoie le point le plus proche de this parmi le segment [A,B]
	 */
	public Point_d getClosestPt (Point_d ptA, Point_d ptB) {
		return UtilsGeom.getClosestPt (ptA, ptB, this);
	}
	

	// Interface ISpatialIndexable
	public double getXMin() {return x;}
	public double getXMax() {return x;}
	public double getYMin() {return y;}
	public double getYMax() {return y;}

	public Rect_d getRectEmprise() {return new Rect_d(this);}

}
